package com.venus.finance.creditdebt.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.venus.common.action.BaseAction;
import com.venus.common.utils.ConditionUtil;
import com.venus.common.utils.Constants;
import com.venus.common.utils.JsonUtil;
import com.venus.finance.account.service.IAccountService;
import com.venus.finance.creditdebt.form.CreditDebtForm;
import com.venus.finance.creditdebt.service.ICreditDebtService;
import com.venus.model.AccountVO;
import com.venus.model.CreditDebtListVO;
import com.venus.model.CreditDebtVO;

public class CreditDebtMultiOperateAction extends BaseAction {

	private ICreditDebtService creditDebtService ;
	
	private IAccountService accountService ;

	public void setCreditDebtService(ICreditDebtService creditDebtService) {
		this.creditDebtService = creditDebtService;
	}
	
	public void setAccountService(IAccountService accountService) {
		this.accountService = accountService;
	}
	
	/**
	 * 分发器
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		String strMethod = objCreditDebtForm.getMethod();
		if("list".equals(strMethod)){
			return this.list(mapping, objCreditDebtForm, request, response);
		}else if("create".equals(strMethod)){
			return this.create(mapping, objCreditDebtForm, request, response);
		}else if("insert".equals(strMethod)){
			return this.insert(mapping, objCreditDebtForm, request, response);
		}else if("edit".equals(strMethod)){
			return this.edit(mapping, objCreditDebtForm, request, response);
		}else if("update".equals(strMethod)){
			return this.update(mapping, objCreditDebtForm, request, response);
		}else if("delete".equals(strMethod)){
			return this.delete(mapping, objCreditDebtForm, request, response);
		}else if("findCreditDebt4Ajax".equals(strMethod)){
			return this.findCreditDebt4Ajax(mapping, objCreditDebtForm, request, response);
		}else if("getCreditDebtAmount".equals(strMethod)){
			return this.getCreditDebtAmount(mapping, objCreditDebtForm, request, response);
		}else if("editCreditDebtList".equals(strMethod)){
			return this.editCreditDebtList(mapping, objCreditDebtForm, request, response);
		}else if("updateList".equals(strMethod)){
			return this.updateList(mapping, objCreditDebtForm, request, response);
		}
		return null;
	}
	
	/**
	 * 列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		this.saveToken(request);
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		List<ConditionUtil> lstConditionUtils = new ArrayList<ConditionUtil>();
		// 只显示当前用户的数据
		lstConditionUtils.add(new ConditionUtil("and", "creator.userId", "=", this.getLoginUser(request).getUserId()+"", false));
		lstConditionUtils.add(new ConditionUtil("and", "creditOrDebt", "=", objCreditDebtForm.getCreditOrDebt()+"", false));
		// 按日期倒序
		lstConditionUtils.add(new ConditionUtil("order by", "deadlineDate", "", "desc", false));
		List<CreditDebtVO> lstCreditDebtVOs = this.creditDebtService.findAllCreditDebt(lstConditionUtils);
		request.setAttribute("lstCreditDebtVOs", lstCreditDebtVOs);
		// 债权或者债务跳转到不同的页面
		if(Constants.CREDITDEBT_CREDIT == objCreditDebtForm.getCreditOrDebt()){
			return mapping.findForward("list_credit");
		}else if(Constants.CREDITDEBT_DEBT == objCreditDebtForm.getCreditOrDebt()){
			return mapping.findForward("list_debt");
		}else{
			return null;
		}
	}
	
	/**
	 * 新增
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		this.saveToken(request);
		// 账户列表
		List<AccountVO> lstAccountVOs = this.accountService.findAccountByUser(this.getLoginUser(request));
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		// 债权或者债务跳转到不同的页面
		if(Constants.CREDITDEBT_CREDIT == objCreditDebtForm.getCreditOrDebt()){
			return mapping.findForward("create_credit");
		}else if(Constants.CREDITDEBT_DEBT == objCreditDebtForm.getCreditOrDebt()){
			return mapping.findForward("create_debt");
		}else{
			return null;
		}
	}
	
	/**
	 * 插入
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward insert(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		// 债权或者债务跳转到不同的页面
		int iCreditDebtId = this.creditDebtService.saveCreditDebt(objCreditDebtForm);
		request.setAttribute("newCreditDebtId", iCreditDebtId);
		return this.list(mapping, form, request, response);
	}
	
	/**
	 * 修改
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		this.saveToken(request);
		// 账户列表
		List<AccountVO> lstAccountVOs = this.accountService.findAccountByUser(this.getLoginUser(request));
		request.setAttribute("lstAccountVOs", lstAccountVOs);
		// 待修改的债权实例
		CreditDebtVO objCreditDebtVO = this.creditDebtService.findCreditDebtVO(objCreditDebtForm.getCreditDebtId());
		request.setAttribute("creditDebtVO", objCreditDebtVO);
		// 明细实例
		Set<CreditDebtListVO> setCreditDebtListVOs = objCreditDebtVO.getCreditDebtList();
		request.setAttribute("setCreditDebtListVOs", setCreditDebtListVOs);
		// 债权或者债务跳转到不同的页面
		if(Constants.CREDITDEBT_CREDIT == objCreditDebtForm.getCreditOrDebt()){
			return mapping.findForward("edit_credit");
		}else if(Constants.CREDITDEBT_DEBT == objCreditDebtForm.getCreditOrDebt()){
			return mapping.findForward("edit_debt");
		}else{
			return null;
		}
	}
	
	/**
	 * 更新
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		// 债权或者债务
		int iCreditDebtId = this.creditDebtService.editCreditDebt(objCreditDebtForm);
		request.setAttribute("updateCreditDebtId", iCreditDebtId);
		return this.list(mapping, form, request, response);
	}
	
	/**
	 * 删除
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		this.creditDebtService.deleteCreditDebt(objCreditDebtForm.getCreditDebtIds());
		return this.list(mapping, form, request, response);
	}
	
	/**
	 * 得到债权债务的实例,作为json返回
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward findCreditDebt4Ajax(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		CreditDebtVO objCreditDebtVO = this.creditDebtService.findCreditDebtVO(objCreditDebtForm.getCreditDebtId());
		return this.forwardAjax(JsonUtil.getJsonString4JavaPOJO(objCreditDebtVO, new String[]{"outAccount","inAccount","creditDebtList","creator"}), response);
	}
	
	/**
	 * 更新借收款金额到债权债务的实例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward getCreditDebtAmount(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		this.creditDebtService.updateCreditDebtAmount(objCreditDebtForm);
		return this.forwardAjax("SUCCESS", response);
	}

	/**
	 * 编辑债权债务的列表实例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward editCreditDebtList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		//CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		return mapping.findForward("editCreditDebtList");
	}
	
	/**
	 * 更新借收款金额到债权债务的实例
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward updateList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response){
		if(!this.isTokenValid(request)){
			return this.list(mapping, form, request, response);
		}
		this.resetToken(request);
		CreditDebtForm objCreditDebtForm = (CreditDebtForm)form;
		String[] strPayDates = request.getParameterValues("payDate");
		String[] strAccountIds = request.getParameterValues("accountId");
		String[] strAmounts = request.getParameterValues("amount");
		String[] strInterests = request.getParameterValues("interest");
		String[] strNotes = request.getParameterValues("note");
		this.creditDebtService.updateCreditDebtList(objCreditDebtForm.getCreditDebtId(), strPayDates, strAccountIds, strAmounts, strInterests, strNotes);
		request.setAttribute("updateCreditDebtId", objCreditDebtForm.getCreditDebtId());
		return this.list(mapping, form, request, response);
	}
}
